<?php 
  
  /**
  * By Abduahad
  * 2017.01.01 1:09 PM
  */
  
  // 上传工具类 
  
  class Uploader
  { 
    public static function main($path,$name){
    
      return self::upload($path,$name);

    }
   
    // 实现分段上传功能  
    private static function upload($path,$name){
       
       $result = array(
           'err' => "Invaled Argument Exception",
           'index' => $_REQUEST['total']
       );
       try {
         
         if(!$name || !$path){
           return $result['err'] = 'Illegal filename or derectory';
         }

         if($data = $_REQUEST){
           $result['err'] = 'no';
           $index = $data['index']; 
           if($index == 1){
              session(['UPLOADING_FILE_NAME'=>$name]);
              if(!is_dir($path)) mkdir($path,0777,true);
           }else{
             $name =  session('UPLOADING_FILE_NAME');
           }
           if($_FILES){
              $orgname = $_REQUEST['name'];
              $type = strrchr($orgname,'.');
              $dir = $path.$name.$type;
              $file = $_FILES['data'];
              $temp = $file['tmp_name'];
              if(!file_exists($dir)){
                move_uploaded_file($file['tmp_name'], $dir);
              }else{ 
               $content = file_get_contents($temp);
               file_put_contents($dir,$content,FILE_APPEND);      
              }
           }
           if($_REQUEST['index'] == $_REQUEST['total']){
             session(['UPLOADING_FILE_NAME'=>null]);
           }
           $result['directory'] = $path.$name.$type;
           $result['index'] = intval($index);
         }

         return $result;
       } catch (Exception $e) {
         return $result;
       }
    } 
  }

 ?>